package com.xiaoye.qbproject.controller.qbzyk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.Area;
import com.xiaoye.qbproject.service.AreaService;
import com.xiaoye.qbproject.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/area")
public class AreaController extends BaseController {
    @Autowired
    private AreaService areaService;

    /**
     * 获取地区下拉列表 带国家列表
     *
     * @return 包含地区和国家列表的AjaxResult对象
     */
    @GetMapping("/getAreaAndCountryList")
    private AjaxResult getAreaAndCountryList() {
        return success(areaService.getAreaDropDownList());
    }

    /**
     * 获取区域列表
     */
    @GetMapping("")
    public AjaxResult list(Area area) {
        String name = area.getName();
        LambdaQueryWrapper<Area> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(Area::getName, name);
        }
        List<Area> areas = areaService.list(queryWrapper);
        return AjaxResult.success(areas);
    }

    /**
     * 获取区域详情
     */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable String id) {
        Area area = areaService.getById(id);
        return AjaxResult.success(area);
    }

    /**
     * 新增区域
     */
    @PostMapping("")
    public AjaxResult add(@RequestBody Area area) {
        return toAjax(areaService.save(area));
    }

    /**
     * 更新区域
     */
    @PutMapping("")
    public AjaxResult update(@RequestBody Area area) {
        return toAjax(areaService.updateById(area));
    }

    /**
     * 删除区域
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id) {
        return toAjax(areaService.removeById(id));
    }
}
